package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 插入订单数据
     * @param orders
     */
    void insertData(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 根据用户 ID 查询订单信息
     * @param userId
     * @return
     */
    @Select("select * from orders where user_id = #{userId}")
    List<Orders> getByUserId(Long userId);

    /**
     * 获取订单基本信息
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders getOrder(Long id);

    /**
     * 服务端查询订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> selectOrder(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 用户查询自己的订单
     * @param ordersPageQueryDTO
     * @return
     */
    Page<OrderVO> selectUserOrder(@Param("dto") OrdersPageQueryDTO ordersPageQueryDTO, @Param("userId") Long userId);

    /**
     * 根据 ID 查询订单菜品信息
     * @param id
     * @return
     */
    Orders selectOrderDish(Long id);


    /**
     * 更改订单状态为 3 已接单
     * @param
     */
    @Update("update orders set status = #{orderStatus} where id = #{orderId}")
    void acceptOrder(Long orderId, Integer orderStatus);


    /**
     * 统计各个状态的订单数量
     * @return
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer orderStatistics(Integer status);

    /**
     * 根据订单状态查询订单
     * @param pendingPayment
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{overdueTime}")
    List<Orders> selectOrderByStatus(@Param("status") Integer pendingPayment, @Param("overdueTime") LocalDateTime overdueTime);
}
